﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MuaDeal
{
    public partial class CapNhatSanPham : Form
    {
        public CapNhatSanPham()
        {
            InitializeComponent();
        }

        private void lbtenmadeal_Click(object sender, EventArgs e)
        {

        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

      
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(listView1.SelectedIndices.Count>0)
            {
                try
                {
                    lbtenmadeal.Text = listView1.Items[listView1.SelectedIndices[0]].Text.ToString();
                    textBox1.Text = listView1.Items[listView1.SelectedIndices[0]].SubItems[1].Text;
                    txtsoluong.Text = listView1.Items[listView1.SelectedIndices[0]].SubItems[2].Text;
                    txtgiaban.Text = listView1.Items[listView1.SelectedIndices[0]].SubItems[3].Text;
                    txtgiadagiam.Text = listView1.Items[listView1.SelectedIndices[0]].SubItems[4].Text;
                    txtngaybatdau.Text = listView1.Items[listView1.SelectedIndices[0]].SubItems[5].Text;
                    txtngayketthuc.Text = listView1.Items[listView1.SelectedIndices[0]].SubItems[6].Text;
                    textBox2.Text = listView1.Items[listView1.SelectedIndices[0]].SubItems[8].Text;
                    cmbtinhtrang.Text= listView1.Items[listView1.SelectedIndices[0]].SubItems[7].Text;

                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Lỗi " + ex.Message);
                }
               
            }
           
        }
        DataTable dttinhtrang = new DataTable();
        DataTable dt = new DataTable();
        DataTable dtlinhvuc = new DataTable();
        public bool kt = false;
        private void CapNhatSanPham_Load(object sender, EventArgs e)
        {
            // Load lên danh sách
            try
            {
                Datahelper helper = new Datahelper();
                helper.Open();
                string proc = "sp_LayDanhSachSanPham";
                if (kt==true)
                {
                    proc = "read_committed_laydanhsach";
                }
                
                string proc1 = "sp_LayTinhTrangSanPham";
                string proc2 = "sp_LayDSLinhVuc";
                dt = helper.ExecuteQueryStore(proc);
                dttinhtrang = helper.ExecuteQueryStore(proc1);
                dtlinhvuc = helper.ExecuteQueryStore(proc2);
                helper.Close();
                foreach (DataRow row in dt.Rows)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = row[0].ToString();
                    item.SubItems.Add(row[2].ToString());
                    item.SubItems.Add(row[7].ToString());
                    item.SubItems.Add(row[3].ToString());
                    item.SubItems.Add(row[4].ToString());
                    item.SubItems.Add(row[5].ToString());
                    item.SubItems.Add(row[6].ToString());
                    
                    foreach (DataRow ttrang in dttinhtrang.Rows)
                    {
                        if (ttrang[0].ToString() == row[8].ToString())
                        {
                            item.SubItems.Add(ttrang[1].ToString());
                        }
                    }
                   
                    item.SubItems.Add(row[9].ToString());
                    listView1.Items.Add(item);
                }
                //Load lên combobox
                string chuoi = "sp_LayDSLinhVuc";
                DataTable dt1 = new DataTable();
                Datahelper hp = new Datahelper();
                hp.Open();
                dt1 = hp.ExecuteQueryStore(chuoi);
                hp.Close();
                cmbloaideal.DataSource = dt1;
                cmbloaideal.DisplayMember = dt1.Columns[1].ToString();
                cmbloaideal.ValueMember = dt1.Columns[0].ToString();

                //Load lên combobox tinhtrang
               
                cmbtinhtrang.DataSource = dttinhtrang;
                cmbtinhtrang.DisplayMember = dttinhtrang.Columns[1].ToString();
                cmbtinhtrang.ValueMember = dttinhtrang.Columns[0].ToString();
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Lỗi " + ex.Message);
            }
        }

        private void cmbloaideal_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        
     
       
        private void lbhienthitheoloai_Click(object sender, EventArgs e)
        {
            try
            {

                Datahelper helper = new Datahelper();
                helper.Open();
                int loai = int.Parse(this.cmbloaideal.SelectedValue.ToString());

                // gọi store có tham số
                  SqlCommand comman = new SqlCommand();
                comman.CommandText = "sp_TimKiemSPTheoLoai";
                comman.CommandType = CommandType.StoredProcedure;
                SqlParameter bien1 = comman.Parameters.Add("@loai", SqlDbType.Int);
                bien1.Direction = ParameterDirection.Input;
                bien1.Value = loai;
                comman.Connection = helper.Connection;

                //lấy ds trả về gán vào listview
                DataTable dttimkiem = new DataTable();
                SqlDataReader reader = comman.ExecuteReader(); 
                listView1.Items.Clear();
                while (reader.Read())
                {


                    ListViewItem item = new ListViewItem();
                    item.Text = reader[0].ToString();
                    item.SubItems.Add(reader[2].ToString());
                    item.SubItems.Add(reader[7].ToString());
                    item.SubItems.Add(reader[3].ToString());
                    item.SubItems.Add(reader[4].ToString());
                    item.SubItems.Add(reader[5].ToString());
                    item.SubItems.Add(reader[6].ToString());

                    foreach (DataRow ttrang in dttinhtrang.Rows)
                    {
                        if (ttrang[0].ToString() == reader[8].ToString())
                        {
                            item.SubItems.Add(ttrang[1].ToString());
                        }
                    }

                    item.SubItems.Add(reader[9].ToString());
                    listView1.Items.Add(item);

                }
                helper.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Lỗi  " + ex.Message);
            }
           
            
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
           

        }

        private void cmbtinhtrang_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void rdbtncodelay_CheckedChanged(object sender, EventArgs e)
        {
        
        }

        private void btncapnhat_Click(object sender, EventArgs e)
        {
            if (rdbtnuncommitted.Checked == true)
            {
                #region Sai Không Có DeLay
                Datahelper helper = new Datahelper();
                helper.Open();
                SqlCommand comman = new SqlCommand();
                //SqlTransaction sqltran=helper.Connection.BeginTransaction();
                //comman.Transaction= sqltran;
                String chuoi = "sp_SuaThongTinSanPhamNoDelay_Sai";
                
                SqlParameter bien1= comman.Parameters.Add("@masp", SqlDbType.Char,10);
                bien1.Direction = ParameterDirection.Input;
                bien1.Value = lbtenmadeal.Text;

               
                 SqlParameter bien10= comman.Parameters.Add("@tensp", SqlDbType.NVarChar,100);
                bien10.Direction = ParameterDirection.Input;
                bien10.Value = textBox1.Text;


                SqlParameter bien3= comman.Parameters.Add("@dg", SqlDbType.Float);
                bien3.Direction = ParameterDirection.Input;
                bien3.Value = txtgiaban.Text;

                 SqlParameter bien4= comman.Parameters.Add("@gdg", SqlDbType.Float);
                bien4.Direction = ParameterDirection.Input;
                bien4.Value = txtgiadagiam.Text;

                  SqlParameter bien5= comman.Parameters.Add("@tgbd", SqlDbType.DateTime);
                bien5.Direction = ParameterDirection.Input;
                bien5.Value = txtngaybatdau.Text;

                 SqlParameter bien9= comman.Parameters.Add("@tgkt", SqlDbType.DateTime);
                bien9.Direction = ParameterDirection.Input;
                bien9.Value = txtngayketthuc.Text;

                 SqlParameter bien6= comman.Parameters.Add("@sl", SqlDbType.Int);
                bien6.Direction = ParameterDirection.Input;
                bien6.Value = txtsoluong.Text;

                SqlParameter bien7= comman.Parameters.Add("@ttsp", SqlDbType.Int);
                bien7.Direction = ParameterDirection.Input;
                bien7.Value =int.Parse(cmbtinhtrang.SelectedValue.ToString());

                 SqlParameter bien8= comman.Parameters.Add("@ctsp", SqlDbType.NVarChar,1000);
                bien8.Direction = ParameterDirection.Input;
                bien8.Value =textBox2.Text;
                comman.CommandText = chuoi;
                comman.CommandType = CommandType.StoredProcedure;
                comman.Connection = helper.Connection;
                int kq=comman.ExecuteNonQuery();
              
                //Nếu cập nhật thành công thì thông báo message box và load lại listview
                if (kq==1)
                {
                    MessageBox.Show("Đã cập nhật sản phẩm thành công");
                    string proc = "sp_LayDanhSachSanPham";
                    string proc1 = "sp_LayTinhTrangSanPham";
                    string proc2 = "sp_LayDSLinhVuc";
                    dt = new DataTable();
                    dtlinhvuc = new DataTable();
                    dttinhtrang = new DataTable();
                    dt = helper.ExecuteQueryStore(proc);
                    dttinhtrang = helper.ExecuteQueryStore(proc1);
                    dtlinhvuc = helper.ExecuteQueryStore(proc2);
                    helper.Close();
                    listView1.Items.Clear();
                    foreach (DataRow row in dt.Rows)
                    {
                        ListViewItem item = new ListViewItem();
                        item.Text = row[0].ToString();
                        item.SubItems.Add(row[2].ToString());
                        item.SubItems.Add(row[7].ToString());
                        item.SubItems.Add(row[3].ToString());
                        item.SubItems.Add(row[4].ToString());
                        item.SubItems.Add(row[5].ToString());
                        item.SubItems.Add(row[6].ToString());

                        foreach (DataRow ttrang in dttinhtrang.Rows)
                        {
                            if (ttrang[0].ToString() == row[8].ToString())
                            {
                                item.SubItems.Add(ttrang[1].ToString());
                            }
                        }

                        item.SubItems.Add(row[9].ToString());
                        listView1.Items.Add(item);
                    }
                }
                else
                {
                   MessageBox.Show("Cập nhật không thành công");
                }
            #endregion
            }
            else if (rdbtnuncommitteddelay.Checked == true)
            {
                #region Uncommitted có delay
                Datahelper helper = new Datahelper();
                helper.Open();
                SqlCommand comman = new SqlCommand();
                //SqlTransaction sqltran=helper.Connection.BeginTransaction();
                //comman.Transaction= sqltran;
                String chuoi = "sp_SuaThongTinSanPhamDelay_Sai";

                SqlParameter bien1 = comman.Parameters.Add("@masp", SqlDbType.Char, 10);
                bien1.Direction = ParameterDirection.Input;
                bien1.Value = lbtenmadeal.Text;


                SqlParameter bien10 = comman.Parameters.Add("@tensp", SqlDbType.NVarChar, 100);
                bien10.Direction = ParameterDirection.Input;
                bien10.Value = textBox1.Text;


                SqlParameter bien3 = comman.Parameters.Add("@dg", SqlDbType.Float);
                bien3.Direction = ParameterDirection.Input;
                bien3.Value = txtgiaban.Text;

                SqlParameter bien4 = comman.Parameters.Add("@gdg", SqlDbType.Float);
                bien4.Direction = ParameterDirection.Input;
                bien4.Value = txtgiadagiam.Text;

                SqlParameter bien5 = comman.Parameters.Add("@tgbd", SqlDbType.DateTime);
                bien5.Direction = ParameterDirection.Input;
                bien5.Value = txtngaybatdau.Text;

                SqlParameter bien9 = comman.Parameters.Add("@tgkt", SqlDbType.DateTime);
                bien9.Direction = ParameterDirection.Input;
                bien9.Value = txtngayketthuc.Text;

                SqlParameter bien6 = comman.Parameters.Add("@sl", SqlDbType.Int);
                bien6.Direction = ParameterDirection.Input;
                bien6.Value = txtsoluong.Text;

                SqlParameter bien7 = comman.Parameters.Add("@ttsp", SqlDbType.Int);
                bien7.Direction = ParameterDirection.Input;
                bien7.Value = int.Parse(cmbtinhtrang.SelectedValue.ToString());

                SqlParameter bien8 = comman.Parameters.Add("@ctsp", SqlDbType.NVarChar, 1000);
                bien8.Direction = ParameterDirection.Input;
                bien8.Value = textBox2.Text;
                comman.CommandText = chuoi;
                comman.CommandType = CommandType.StoredProcedure;
                comman.Connection = helper.Connection;
                int kq = comman.ExecuteNonQuery();

                //Nếu cập nhật thành công thì thông báo message box và load lại listview
                if (kq == 1)
                {
                    MessageBox.Show("Đã cập nhật sản phẩm thành công");
                    string proc = "sp_LayDanhSachSanPham";
                    string proc1 = "sp_LayTinhTrangSanPham";
                    string proc2 = "sp_LayDSLinhVuc";
                    dt = new DataTable();
                    dtlinhvuc = new DataTable();
                    dttinhtrang = new DataTable();
                    dt = helper.ExecuteQueryStore(proc);
                    dttinhtrang = helper.ExecuteQueryStore(proc1);
                    dtlinhvuc = helper.ExecuteQueryStore(proc2);
                    helper.Close();
                    listView1.Items.Clear();
                    foreach (DataRow row in dt.Rows)
                    {
                        ListViewItem item = new ListViewItem();
                        item.Text = row[0].ToString();
                        item.SubItems.Add(row[2].ToString());
                        item.SubItems.Add(row[7].ToString());
                        item.SubItems.Add(row[3].ToString());
                        item.SubItems.Add(row[4].ToString());
                        item.SubItems.Add(row[5].ToString());
                        item.SubItems.Add(row[6].ToString());

                        foreach (DataRow ttrang in dttinhtrang.Rows)
                        {
                            if (ttrang[0].ToString() == row[8].ToString())
                            {
                                item.SubItems.Add(ttrang[1].ToString());
                            }
                        }
                        item.SubItems.Add(row[9].ToString());
                        listView1.Items.Add(item);
                    }
                }
                    else
                    {
                        MessageBox.Show("Cập nhật không thành công");
                    }
                #endregion
            }
            else if(rdbtndungkodelay.Checked==true)
            {
                #region Read committed ko delay
                Datahelper helper = new Datahelper();
                helper.Open();
                SqlCommand comman = new SqlCommand();
                //SqlTransaction sqltran=helper.Connection.BeginTransaction();
                //comman.Transaction= sqltran;
                String chuoi = "sp_SuaThongTinSanPhamNoDeLay_Dung";

                SqlParameter bien1 = comman.Parameters.Add("@masp", SqlDbType.Char, 10);
                bien1.Direction = ParameterDirection.Input;
                bien1.Value = lbtenmadeal.Text;


                SqlParameter bien10 = comman.Parameters.Add("@tensp", SqlDbType.NVarChar, 100);
                bien10.Direction = ParameterDirection.Input;
                bien10.Value = textBox1.Text;


                SqlParameter bien3 = comman.Parameters.Add("@dg", SqlDbType.Float);
                bien3.Direction = ParameterDirection.Input;
                bien3.Value = txtgiaban.Text;

                SqlParameter bien4 = comman.Parameters.Add("@gdg", SqlDbType.Float);
                bien4.Direction = ParameterDirection.Input;
                bien4.Value = txtgiadagiam.Text;

                SqlParameter bien5 = comman.Parameters.Add("@tgbd", SqlDbType.DateTime);
                bien5.Direction = ParameterDirection.Input;
                bien5.Value = txtngaybatdau.Text;

                SqlParameter bien9 = comman.Parameters.Add("@tgkt", SqlDbType.DateTime);
                bien9.Direction = ParameterDirection.Input;
                bien9.Value = txtngayketthuc.Text;

                SqlParameter bien6 = comman.Parameters.Add("@sl", SqlDbType.Int);
                bien6.Direction = ParameterDirection.Input;
                bien6.Value = txtsoluong.Text;

                SqlParameter bien7 = comman.Parameters.Add("@ttsp", SqlDbType.Int);
                bien7.Direction = ParameterDirection.Input;
                bien7.Value = int.Parse(cmbtinhtrang.SelectedValue.ToString());

                SqlParameter bien8 = comman.Parameters.Add("@ctsp", SqlDbType.NVarChar, 1000);
                bien8.Direction = ParameterDirection.Input;
                bien8.Value = textBox2.Text;
                comman.CommandText = chuoi;
                comman.CommandType = CommandType.StoredProcedure;
                comman.Connection = helper.Connection;
                int kq = comman.ExecuteNonQuery();

                //Nếu cập nhật thành công thì thông báo message box và load lại listview
                if (kq == 1)
                {
                    MessageBox.Show("Đã cập nhật sản phẩm thành công");
                    string proc = "sp_LayDanhSachSanPham";
                    string proc1 = "sp_LayTinhTrangSanPham";
                    string proc2 = "sp_LayDSLinhVuc";
                    dt = new DataTable();
                    dtlinhvuc = new DataTable();
                    dttinhtrang = new DataTable();
                    dt = helper.ExecuteQueryStore(proc);
                    dttinhtrang = helper.ExecuteQueryStore(proc1);
                    dtlinhvuc = helper.ExecuteQueryStore(proc2);
                    helper.Close();
                    listView1.Items.Clear();
                    foreach (DataRow row in dt.Rows)
                    {
                        ListViewItem item = new ListViewItem();
                        item.Text = row[0].ToString();
                        item.SubItems.Add(row[2].ToString());
                        item.SubItems.Add(row[7].ToString());
                        item.SubItems.Add(row[3].ToString());
                        item.SubItems.Add(row[4].ToString());
                        item.SubItems.Add(row[5].ToString());
                        item.SubItems.Add(row[6].ToString());

                        foreach (DataRow ttrang in dttinhtrang.Rows)
                        {
                            if (ttrang[0].ToString() == row[8].ToString())
                            {
                                item.SubItems.Add(ttrang[1].ToString());
                            }
                        }
                        item.SubItems.Add(row[9].ToString());
                        listView1.Items.Add(item);
                    }
                }
                else
                {
                    MessageBox.Show("Cập nhật không thành công");
                }
                #endregion
            }
            else if (rdbtndungcodelay.Checked == true)
            {
                #region Read committed có delay
                Datahelper helper = new Datahelper();
                helper.Open();
                SqlCommand comman = new SqlCommand();
                //SqlTransaction sqltran=helper.Connection.BeginTransaction();
                //comman.Transaction= sqltran;
                String chuoi = "sp_SuaThongTinSanPhamDelay_Dung";

                SqlParameter bien1 = comman.Parameters.Add("@masp", SqlDbType.Char, 10);
                bien1.Direction = ParameterDirection.Input;
                bien1.Value = lbtenmadeal.Text;


                SqlParameter bien10 = comman.Parameters.Add("@tensp", SqlDbType.NVarChar, 100);
                bien10.Direction = ParameterDirection.Input;
                bien10.Value = textBox1.Text;


                SqlParameter bien3 = comman.Parameters.Add("@dg", SqlDbType.Float);
                bien3.Direction = ParameterDirection.Input;
                bien3.Value = txtgiaban.Text;

                SqlParameter bien4 = comman.Parameters.Add("@gdg", SqlDbType.Float);
                bien4.Direction = ParameterDirection.Input;
                bien4.Value = txtgiadagiam.Text;

                SqlParameter bien5 = comman.Parameters.Add("@tgbd", SqlDbType.DateTime);
                bien5.Direction = ParameterDirection.Input;
                bien5.Value = txtngaybatdau.Text;

                SqlParameter bien9 = comman.Parameters.Add("@tgkt", SqlDbType.DateTime);
                bien9.Direction = ParameterDirection.Input;
                bien9.Value = txtngayketthuc.Text;

                SqlParameter bien6 = comman.Parameters.Add("@sl", SqlDbType.Int);
                bien6.Direction = ParameterDirection.Input;
                bien6.Value = txtsoluong.Text;

                SqlParameter bien7 = comman.Parameters.Add("@ttsp", SqlDbType.Int);
                bien7.Direction = ParameterDirection.Input;
                bien7.Value = int.Parse(cmbtinhtrang.SelectedValue.ToString());

                SqlParameter bien8 = comman.Parameters.Add("@ctsp", SqlDbType.NVarChar, 1000);
                bien8.Direction = ParameterDirection.Input;
                bien8.Value = textBox2.Text;
                comman.CommandText = chuoi;
                comman.CommandType = CommandType.StoredProcedure;
                comman.Connection = helper.Connection;
                int kq = comman.ExecuteNonQuery();

                //Nếu cập nhật thành công thì thông báo message box và load lại listview
                if (kq == 1)
                {
                    MessageBox.Show("Đã cập nhật sản phẩm thành công");
                    string proc = "sp_LayDanhSachSanPham";
                    string proc1 = "sp_LayTinhTrangSanPham";
                    string proc2 = "sp_LayDSLinhVuc";
                    dt = new DataTable();
                    dtlinhvuc = new DataTable();
                    dttinhtrang = new DataTable();
                    dt = helper.ExecuteQueryStore(proc);
                    dttinhtrang = helper.ExecuteQueryStore(proc1);
                    dtlinhvuc = helper.ExecuteQueryStore(proc2);
                    helper.Close();
                    listView1.Items.Clear();
                    foreach (DataRow row in dt.Rows)
                    {
                        ListViewItem item = new ListViewItem();
                        item.Text = row[0].ToString();
                        item.SubItems.Add(row[2].ToString());
                        item.SubItems.Add(row[7].ToString());
                        item.SubItems.Add(row[3].ToString());
                        item.SubItems.Add(row[4].ToString());
                        item.SubItems.Add(row[5].ToString());
                        item.SubItems.Add(row[6].ToString());

                        foreach (DataRow ttrang in dttinhtrang.Rows)
                        {
                            if (ttrang[0].ToString() == row[8].ToString())
                            {
                                item.SubItems.Add(ttrang[1].ToString());
                            }
                        }
                        item.SubItems.Add(row[9].ToString());
                        listView1.Items.Add(item);
                    }
                }
                else
                {
                    MessageBox.Show("Cập nhật không thành công");
                }
                #endregion
            }
        }

        private void rdbtnkodelay_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btntatca_Click(object sender, EventArgs e)
        {
              try
            {
                Datahelper helper = new Datahelper();
                helper.Open();
                string proc = "sp_LayDanhSachSanPham";
                if (kt==true)
                {
                    proc = "read_committed_laydanhsach";
                }
                
                string proc1 = "sp_LayTinhTrangSanPham";
                string proc2 = "sp_LayDSLinhVuc";
                dt = helper.ExecuteQueryStore(proc);
                dttinhtrang = helper.ExecuteQueryStore(proc1);
                dtlinhvuc = helper.ExecuteQueryStore(proc2);
                helper.Close();
                listView1.Items.Clear();
                foreach (DataRow row in dt.Rows)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = row[0].ToString();
                    item.SubItems.Add(row[2].ToString());
                    item.SubItems.Add(row[7].ToString());
                    item.SubItems.Add(row[3].ToString());
                    item.SubItems.Add(row[4].ToString());
                    item.SubItems.Add(row[5].ToString());
                    item.SubItems.Add(row[6].ToString());
                    
                    foreach (DataRow ttrang in dttinhtrang.Rows)
                    {
                        if (ttrang[0].ToString() == row[8].ToString())
                        {
                            item.SubItems.Add(ttrang[1].ToString());
                        }
                    }
                   
                    item.SubItems.Add(row[9].ToString());
                    listView1.Items.Add(item);
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Lỗi " + ex.Message);
            }
        }

        private void rdbtndungkodelay_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
        
}
